#pragma once

#include <QObject>
#include <memory>
#include <QTimer>
#include "dbclinethandler.h"
#include "remotetablemodel.h"

class LogManagementPrivate
{
public:
    /// @brief 数据库连接状态
    bool currentDBState = false;
    /// @brief 数据库连接定时器
    QTimer *healthDBTimer;
    std::shared_ptr<DBClientThreadHandler> client;
    RemoteTableModel *operationLogModel; // 操作日志

    RemoteTableModel *DZCSModel;    // 底砖吹扫
    RemoteTableModel *DZCSIMGModel; // 底砖吹扫 - 图片
    RemoteTableModel *YGCZModel;    // 油缸拆装
    RemoteTableModel *SKSYModel;    // 水口烧眼
    RemoteTableModel *SKSYIMGModel; // 水口烧眼 - 图片
    RemoteTableModel *SKCZModel;    // 水口拆装
    RemoteTableModel *SKCZIMGModel; // 水口拆装 - 图片
    RemoteTableModel *JYLSModel;    // 加引流砂
    RemoteTableModel *JYLSIMGModel; // 加引流砂- 图片
    RemoteTableModel *matterModel;  // 设备检修
    RemoteTableModel *eventModel;   // 事件处理
    RemoteTableModel *analyModel;   // 分析处理
};

class LogManagement : public QObject
{
    Q_OBJECT
public:
    static LogManagement &getInstance();

    RemoteTableModel *getOperationLogModel();

    RemoteTableModel *getDZCSModel();
    RemoteTableModel *getDZCSIMGModel();
    RemoteTableModel *getYGCZModel();
    RemoteTableModel *getSKSYModel();
    RemoteTableModel *getSKSYIMGModel();
    RemoteTableModel *getSKCZModel();
    RemoteTableModel *getSKCZIMGModel();
    RemoteTableModel *getJYLSModel();
    RemoteTableModel *getJYLSIMGModel();
    RemoteTableModel *getMatterModel();
    RemoteTableModel *getEventModel();
    RemoteTableModel *getAnalyModel();

signals:
    void databaseReconnected();  // 数据库重新连接信号
    void databaseDisconnected(); // 数据库断开连接信号

private:
    LogManagement(QObject *parent = nullptr);
    ~LogManagement();

    /// 初始化数据库
    void initClient();
    /// 初始化模型
    void initModels();
    /// @brief 断开连接处理-DB
    void handleDBDisconnection();
    /// @brief 重新连接处理-DB
    void handleDBReconnection();

    /// @brief 连接数据库异步检查
    void checkDatabaseStateAsync();
    /// @brief 处理数据库健康检查结果
    void handleHealthCheckResult(bool isConnected);

private:
    std::unique_ptr<LogManagementPrivate> dataPtr;
};